package org.playground.booking.dao.impl;

import org.hibernate.query.Query;
import org.playground.booking.dao.IPlaygroundDao;
import org.playground.booking.dao.IUserDao;
import org.playground.booking.entity.Playground;
import org.playground.booking.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by root on 17-10-28.
 */
@Repository
public class PlaygroundDaoImpl extends AbstractDaoImpl<Playground,Long> implements IPlaygroundDao {

    public List<Playground> readPlaygroundForSport(int sportId) {
        String hql ="select pg from Playground as pg " +
                " inner join pg.playgroundType as pgt " +
                " where pgt.id=:sportId";

        Query query = this.getCurrentSession().createQuery(hql);
        query.setParameter("sportId",sportId);
        return query.list();
    }

    @Override
    public List<Playground> readPlaygroundForMerchant(Long merchantId, int sportId) {
        String hql;

        if(sportId<=0){
            hql ="select pg from Playground as pg " +
                    " inner join pg.merchant as merchant " +
                    " where merchant.id=:merchantId";

        }else{
            hql ="select pg from Playground as pg " +
                    " inner join pg.playgroundType as pgt" +
                    " inner join pg.merchant as merchant " +
                    " where pgt.id=:sportId and merchant.id=:merchantId";

        }

        Query query = this.getCurrentSession().createQuery(hql);
        if(sportId!=0){
            query.setParameter("sportId",sportId);
        }
        query.setParameter("merchantId",merchantId);
        return query.list();
    }


}
